package org.example.http;

import org.apache.commons.lang3.StringUtils;
import org.example.LandEntity;

import java.util.ArrayList;
import java.util.List;

import static org.example.constant.Common.RASTER_URL;
import static org.example.constant.Common.TOKEN;

public class ParamEntity {
    String name;
    String token;
    List<Land> list;
    String zzjgyx;
    String type;
    String zwmc;
    public static class Land{
      private int objectid;
      private String code;
      private String farmerName;
      private String shape;
      private double x;
      private double y;

        public int getObjectid() {
            return objectid;
        }

        public void setObjectid(int objectid) {
            this.objectid = objectid;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getFarmerName() {
            return farmerName;
        }

        public void setFarmerName(String farmerName) {
            this.farmerName = farmerName;
        }

        public String getShape() {
            return shape;
        }

        public void setShape(String shape) {
            this.shape = shape;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public List<Land> getList() {
        return list;
    }

    public void setList(List<Land> list) {
        this.list = list;
    }

    public String getZzjgyx() {
        return zzjgyx;
    }

    public void setZzjgyx(String zzjgyx) {
        this.zzjgyx = zzjgyx;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getZwmc() {
        return zwmc;
    }

    public void setZwmc(String zwmc) {
        this.zwmc = zwmc;
    }

    public static ParamEntity fromLandEntity(List<LandEntity>  lands,  String zwmc,  String ygyxid){
          ParamEntity paramEntity = new ParamEntity();
          paramEntity.setToken(TOKEN);
          paramEntity.setType("2");
          paramEntity.setZwmc(zwmc);
          List< Land> list = new ArrayList<>();
        for (int i = 0; i < lands.size(); i++) {
            Land land = new Land();
            land.setObjectid(i);
            //将i补足4位
            land.setCode(String.format("%04d", i));
            land.setFarmerName(lands.get(i).getNhxm());
            land.setX(lands.get(i).getX());
            land.setY(lands.get(i).getY());
            land.setShape(lands.get(i).getGeoJson());
            list.add(land);
        }
        paramEntity.setZzjgyx(RASTER_URL.replace("{raster_id}",ygyxid));
        paramEntity.setList(list);
        return paramEntity;
    }
    public static ParamEntity fromLandEntity(List<LandEntity>  lands, String qhmc, String zwmc, String nf){
        ParamEntity paramEntity = new ParamEntity();
        paramEntity.setName(qhmc+nf+"年"+zwmc+"散户承保分布图");
        paramEntity.setToken(TOKEN);
        paramEntity.setType("1");
        List< Land> list = new ArrayList<>();
        for (int i = 0; i < lands.size(); i++) {
            Land land = new Land();
            land.setObjectid(i);
            //将i补足4位
            if(StringUtils.isBlank(lands.get(i).getNhqqdm())){
                lands.get(i).setNhqqdm(String.format("%04d", i+1));
                land.setCode(String.format("%04d", i+1));
            }else {
                //提取最后四位
                land.setCode(lands.get(i).getNhqqdm().substring(lands.get(i).getNhqqdm().length()-4));
            }

            land.setFarmerName(lands.get(i).getNhxm());
            land.setX(lands.get(i).getX());
            land.setY(lands.get(i).getY());
            land.setShape(lands.get(i).getGeoJson());
            list.add(land);

        }
        paramEntity.setList(list);
        return paramEntity;
    }
}
